package cn.com.egova.mahjong.util;

import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.egova.mahjong.R;
import cn.com.egova.mahjong.bean.MahjongBO;
import cn.com.egova.mahjong.dao.MahjongDao;
import cn.com.egova.mahjong.inter.ICallBack;

public class MahjongTool {
    public static final String TYPE_WAN = "wan";
    public static final String TYPE_TONG = "tong";
    public static final String TYPE_TIAO = "tiao";
    public static final String TYPE_FENG = "feng";
    private Context context;
    private MahjongDao dao;

    public MahjongTool(Context context, MahjongDao dao) {
        this.context = context;
        this.dao = dao;
    }

    public void showWithImages(LinearLayout container, MahjongBO bo) {
        String wan = bo.getWan();
        String tong = bo.getTong();
        String tiao = bo.getTiao();
        String feng = bo.getFeng();
        List<Integer> listRes = new ArrayList<>();
        List<Integer> listWan = getResId("wan", wan);
        List<Integer> listTong = getResId("tong", tong);
        List<Integer> listTiao = getResId("tiao", tiao);
        List<Integer> listFeng = getResId("feng", feng);
        listRes.addAll(listWan);
        listRes.addAll(listTong);
        listRes.addAll(listTiao);
        listRes.addAll(listFeng);
        container.removeAllViews();
        for (int resId : listRes) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(resId);
            if (listRes.size() >= 12) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(53, 66);
                imageView.setLayoutParams(params);
            }
            container.addView(imageView);
        }
    }


    public List<Integer> getResId(String type, String values) {
        List<Integer> resIds = new ArrayList<>();
        if (TextUtils.isEmpty(values)) {
            return resIds;
        }
        if (TYPE_WAN.equals(type) || TYPE_TONG.equals(type) || TYPE_TIAO.equals(type)) {
            for (int i = 0; i < values.length(); i++) {
                String s = values.substring(i, i + 1);
                String resName = type + "_" + s;
                int resId = context.getResources().getIdentifier(resName, "mipmap", context.getPackageName());
                resIds.add(resId);
            }
        } else {
            for (int i = 0; i < values.length(); i++) {
                String s = values.substring(i, i + 1);
                String resName = type + "_" + getPinyin(s);
                int resId = context.getResources().getIdentifier(resName, "mipmap", context.getPackageName());
                resIds.add(resId);
            }
        }

        return resIds;
    }

    public String getPinyin(String feng) {
        String result = null;
        if ("东".equals(feng)) {
            result = "dong";
        } else if ("南".equals(feng)) {
            result = "nan";
        } else if ("西".equals(feng)) {
            result = "xi";
        } else if ("北".equals(feng)) {
            result = "bei";
        } else if ("中".equals(feng)) {
            result = "zhong";
        } else if ("发".equals(feng)) {
            result = "fa";
        } else if ("白".equals(feng)) {
            result = "bai";
        }
        return result;
    }


    public void addData(final String type, final ICallBack<MahjongBO> callBack) {
        final int maxId = dao.getMaxId();  //当前最大的id值
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请输入数据");
        final AlertDialog dialog = builder.create();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_add, null);
        final EditText etWan = view.findViewById(R.id.et_wan);
        final EditText etTong = view.findViewById(R.id.et_tong);
        final EditText etTiao = view.findViewById(R.id.et_tiao);
        final EditText etFeng = view.findViewById(R.id.et_feng);
        final EditText etTing = view.findViewById(R.id.et_ting);
        final TextView tvTing = view.findViewById(R.id.tv_ting);
        if("典型牌例".equals(type)){
            tvTing.setText("策略");
        }

        Button btnOK = view.findViewById(R.id.btn_ok);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String wan = etWan.getText().toString().trim();
                String tong = etTong.getText().toString().trim();
                String tiao = etTiao.getText().toString().trim();
                String feng = etFeng.getText().toString().trim();
                String ting = etTing.getText().toString().trim();
                MahjongBO bo = new MahjongBO();
                bo.setId(maxId + 1);
                bo.setType(type);
                bo.setDisplayOrder(maxId + 1);
                bo.setWan(wan);
                bo.setTong(tong);
                bo.setTiao(tiao);
                bo.setFeng(feng);
                bo.setTing(ting);
                if (callBack != null) {
                    callBack.onResult(-1, bo);
                }
                dialog.dismiss();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }

    public void updateData(final MahjongBO bo, final ICallBack<MahjongBO> callBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请编辑数据");
        final AlertDialog dialog = builder.create();
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_add, null);
        final EditText etWan = view.findViewById(R.id.et_wan);
        final EditText etTong = view.findViewById(R.id.et_tong);
        final EditText etTiao = view.findViewById(R.id.et_tiao);
        final EditText etFeng = view.findViewById(R.id.et_feng);
        final EditText etTing = view.findViewById(R.id.et_ting);
        final TextView tvTing = view.findViewById(R.id.tv_ting);
        if("典型牌例".equals(bo.getType())){
            tvTing.setText("策略");
        }

        etWan.setText(TextUtils.isEmpty(bo.getWan()) ? "" : bo.getWan());
        etTong.setText(TextUtils.isEmpty(bo.getTong()) ? "" : bo.getTong());
        etTiao.setText(TextUtils.isEmpty(bo.getTiao()) ? "" : bo.getTiao());
        etFeng.setText(TextUtils.isEmpty(bo.getFeng()) ? "" : bo.getFeng());
        etTing.setText(TextUtils.isEmpty(bo.getTing()) ? "" : bo.getTing());
        Button btnOK = view.findViewById(R.id.btn_ok);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String wan = etWan.getText().toString().trim();
                String tong = etTong.getText().toString().trim();
                String tiao = etTiao.getText().toString().trim();
                String feng = etFeng.getText().toString().trim();
                String ting = etTing.getText().toString().trim();
                bo.setWan(wan);
                bo.setTong(tong);
                bo.setTiao(tiao);
                bo.setFeng(feng);
                bo.setTing(ting);
                if (callBack != null) {
                    callBack.onResult(-1, bo);
                }
                dialog.dismiss();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }
}
